const goodsID = location.href.match(/\/id\/(\d+)/)[1]
const zhengxinBox = $('.zhengxin_check_box')
const zhengxinYuqiBox = $('.yuqi_check_box')

$(document).ready(() => {
  const compentName = `x-zhengxin`
  const addZhengxinURL = String($('#post_new_zhengxin_require').val())
  const addZhengxinYuqiURL = String($('#post_new_zhengxin_yuqi_require').val())
  // const removeZhengxinURL = String($('#post_remove_zhengxin_require').val())
  // const removeZhengxinYuqiURL = String($('#post_remove_zhengxin_yuqi_require').val())
  const updataZhengxinURL = String($('#post_updata_zhengxin_require').val())
  const updataZhengxinYuqiURL = String($('#post_updata_zhengxin_yuqi_require').val())

  const URLObject = {
    addZhengxinURL: addZhengxinURL,
    addZhengxinYuqiURL: addZhengxinYuqiURL,
    // removeZhengxinURL: removeZhengxinURL,
    // removeZhengxinYuqiURL: removeZhengxinYuqiURL,
    updataZhengxinURL: updataZhengxinURL,
    updataZhengxinYuqiURL: updataZhengxinYuqiURL
  }
  // 建立添加item的listener
  $('.add_more_zhengxin_item').on({click: addMoreZhengxinItem}, {url: URLObject})
  $('.add_more_yuqi_item').on({click: addMoreYuqiItem}, {url: URLObject})
  // 初始化颜色选择
  $('.zhengxin_check_color').each((index, item) => {
    $(item).colorPicker()
  })
  // 回填征信
  try {
    rewriteZhengxin()
  } catch (err) {
    console.log(err)
  }
  try {
    rewriteZhengxinYuqi()
  } catch (err) {
    console.log(err)
  }
  // 初始化征信
  $('#zhengxin_check_if').one('change', function (event) {
    if ($(event.target).closest('.zhengxin_check_line').find('.zhengxin_check_line_id').length > 0) {
      throw new Error('log: 存在征信消息id，判断已经有回填，不需要进行初始化请求')
    }
    const goodsID = location.href.match(/\/id\/(\d+)/)[1]
    const addZhengxinURL = String($('#post_new_zhengxin_require').val())
    console.log('请求一行新的征信要求，发送：')
    const postData = {
      zhengxin_check_if: '',
      zhengxin_check_count_method: '',
      zhengxin_check_count_cycle: '',
      zhengxin_check_count_time: '',
      cinv_times_color: '',
      g_id: goodsID,
      state: 1
    }
    console.dir(postData)
    $.ajax(addZhengxinURL, {
      method: 'post',
      data: postData
    }).done((data) => {
      if (String(data.code) === '1' || String(data.code) === '2') {
        // 请求成功
        const templ = `
          <input type="num" name="zhengxin_check_count_cycle" id="zhengxin_check_count_cycle" placeholder="1~36个月"/>
          <div class="placeholder">不能超过</div>
          <select name="zhengxin_check_count_time" id="zhengxin_check_count_time">
            <option value="1" checked>1次</option>
            <option value="2">2次</option>
            <option value="3">3次</option>
            <option value="4">4次</option>
            <option value="5">5次</option>
            <option value="6">6次</option>
            <option value="7">7次</option>
            <option value="8">8次</option>
            <option value="9">9次</option>
            <option value="10">10次</option>
            <option value="11">11次</option>
            <option value="12">12次</option>
            <option value="13">13次</option>
            <option value="14">14次</option>
            <option value="15">15次</option>
            <option value="16">16次</option>
            <option value="17">17次</option>
            <option value="18">18次</option>
            <option value="19">19次</option>
            <option value="20">20次或以上</option>
          </select>
          <div class="add_more_zhengxin_item"><img src="/Application/Admin/View/Public/images/set_chanpin_popup/u546.png" alt="u546"></div>
          <input type="hidden" class="zhengxin_check_line_id" name="zhengxin_check_line_id" value='${data.cinv_times_id}'>
          <input form='hidden_color_placeholder' class='zhengxin_check_color' id="zhengxin_check_1" type="hidden" name="color" value="#000" />
          <script>
            const addZhengxinURL = String($('#post_new_zhengxin_require').val())
            $('#zhengxin_check_1').colorPicker()
            $('.add_more_zhengxin_item').on({click: addMoreZhengxinItem}, {
              url: {
                addZhengxinURL: addZhengxinURL,
              }
            })
          </script>
        `.trim()
        $('.zhengxin_check_line').append(templ)
      } else {
        // 请求无法完成
        console.log(data)
      }
    }).fail((err) => {
      // 网络错误
      console.log(err)
    })
  })
  // 初始化征信逾期
  $('#yuqi_check_if').one('change', function (event) {
    if ($(event.target).closest('.yuqi_check_line').find('.yuqi_check_line_id').length > 0) {
      throw new Error('log: 存在征信逾期消息id，判断已经有回填，不需要进行初始化请求')
    }
    const goodsID = location.href.match(/\/id\/(\d+)/)[1]
    const addZhengxinURL = String($('#post_new_zhengxin_yuqi_require').val())
    console.log('请求一行新的征信逾期要求，发送：')
    const postData = {
      yuqi_check_if: '',
      yuqi_check_count_method: '',
      yuqi_check_count_cycle: '',
      yuqi_check_count_time: '',
      yuqi_check_count_source: '',
      cinv_times_color: '',
      g_id: goodsID,
      state: 1
    }
    console.dir(postData)
    $.ajax(addZhengxinURL, {
      method: 'post',
      data: postData
    }).done((data) => {
      if (String(data.code) === '1' || String(data.code) === '2') {
        // 请求成功
        const templ = `
          <input type="num" name="yuqi_check_count_cycle" id="yuqi_check_count_cycle" placeholder="1~36个月"/>
          <div class="placeholder">不能</div>
          <select name="yuqi_check_count_method" id="yuqi_check_count_method">
            <option value="连续">连续</option>
            <option value="累计">累计</option>
          </select>
          <select name="yuqi_check_count_time" id="yuqi_check_count_time">
            <option value="1" checked>1个</option>
            <option value="2">2个</option>
            <option value="3">3个</option>
            <option value="4">4个</option>
            <option value="5">5个</option>
            <option value="6">6个</option>
            <option value="7">7个</option>
            <option value="8">8个</option>
            <option value="9">9个</option>
            <option value="10">10个</option>
            <option value="11">11个</option>
            <option value="12">12个</option>
            <option value="13">13个</option>
            <option value="14">14个</option>
            <option value="15">15个</option>
            <option value="16">16个</option>
            <option value="17">17个</option>
            <option value="18">18个</option>
            <option value="19">19个</option>
            <option value="20">20个</option>
          </select>
          <select name="yuqi_check_count_source" id="yuqi_check_count_source">
            <option value="1" checked>1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
            <option value="6">6</option>
            <option value="7">7</option>
          </select>
          <div class="add_more_yuqi_item"><img src="/Application/Admin/View/Public/images/set_chanpin_popup/u546.png" alt="u546"></div>
          <input type="hidden" class="yuqi_check_line_id" name="yuqi_check_line_id" value='${data.cinv_see_id}'>
          <input form='hidden_color_placeholder' class='zhengxin_check_color' id="yuqi_check_1" type="hidden" name="color" value="#000" />
          <script>
            const addZhengxinYuqiURL = String($('#post_new_zhengxin_yuqi_require').val())
            const URLObject = {
              addZhengxinYuqiURL: addZhengxinYuqiURL,
            }
            $('#yuqi_check_1').colorPicker()
            $('.add_more_yuqi_item').on({click: addMoreYuqiItem}, {url: URLObject})
          </script>
        `.trim()
        $('.yuqi_check_line').append(templ)
      } else {
        // 请求无法完成
        console.log(data)
      }
    }).fail((err) => {
      // 网络错误
      console.log(err)
    })
  })
  // 征信逾期在选择为不显示时，隐藏右边的
  $('#zhengxin_check_if').on('change', (evt) => {
    if(String($(evt.target).val()) === '0') {
      $('.zhengxin_check_line').each((index, item) => {
        if (index === 0) {
          // 第一行留下第一个
          $(item).children('div,input,select').each((subindex, subitem) => {
            if (subindex > 1) {
              $(subitem).css('display', 'none')
            }
          })
        } else {
          $(item).css('display', 'none')
        }
      })
    } else {
      $('.zhengxin_check_line').each((index, item) => {
        if (index === 0) {
          // 第一行留下第一个
          $(item).children('div,input,select').each((subindex, subitem) => {
            if (subindex > 1) {
              $(subitem).css('display', 'block')
            }
          })
        } else {
          $(item).css('display', 'flex')
        }
      })
    }
  })
  $('#yuqi_check_if').on('change', (evt) => {
    if(String($(evt.target).val()) === '0') {
      $('.yuqi_check_line').each((index, item) => {
        if (index === 0) {
          // 第一行留下第一个
          $(item).children('div,input,select').each((subindex, subitem) => {
            if (subindex > 0) {
              $(subitem).css('display', 'none')
            }
          })
        } else {
          $(item).css('display', 'none')
        }
      })
    } else {
      $('.yuqi_check_line').each((index, item) => {
        if (index === 0) {
          // 第一行留下第一个
          $(item).children('div,input,select').each((subindex, subitem) => {
            if (subindex > 0) {
              $(subitem).css('display', 'block')
            }
          })
        } else {
          $(item).css('display', 'flex')
        }
      })
    }
  })
})

function addMoreZhengxinItem (evt) {
  console.log('请求一行新的征信要求，发送：')
  const postData = {
    zhengxin_check_if: $(`#zhengxin_check_if`).val(),
    zhengxin_check_count_method: $(`#zhengxin_check_count_method`).val(),
    zhengxin_check_count_cycle: $(`#zhengxin_check_count_cycle`).val(),
    zhengxin_check_count_time: $(`#zhengxin_check_count_time`).val(),
    cinv_times_color: $(`#zhengxin_check_1`).val(),
    g_id: goodsID,
    state: $('.zhengxin_check_line').length === 1 ? 1 : 0
  }
  // const postData = $('#zhengxin_hidden_form').serializeArray()
  console.dir(postData)
  // throw new Error(`1`)
  $.ajax(evt.data.url.addZhengxinURL, {
    method: 'post',
    data: postData,
    beforeSend: () => {
      $('.add_more_zhengxin_item').css('display', 'none')
    }
  }).done((data) => {
    $('.add_more_zhengxin_item').css('display', 'block')
    if (String(data.code) === '1' || String(data.code) === '2') {
      // 请求成功
      const templ = `
        <div class="zhengxin_check_line">
          <div style="width:27.5rem;"></div>
          <input type="num" id='zhengxin_check_count_cycle_${data.cinv_times_id}' name="zhengxin_check_count_cycle" placeholder="1~36个月"/>
          <div class="placeholder">不能超过</div>
          <select name="zhengxin_check_count_time" id='zhengxin_check_count_time_${data.cinv_times_id}'>
            <option value="1">1次</option>
            <option value="2">2次</option>
            <option value="3">3次</option>
            <option value="4">4次</option>
            <option value="5">5次</option>
            <option value="6">6次</option>
            <option value="7">7次</option>
            <option value="8">8次</option>
            <option value="9">9次</option>
            <option value="10">10次</option>
            <option value="11">11次</option>
            <option value="12">12次</option>
            <option value="13">13次</option>
            <option value="14">14次</option>
            <option value="15">15次</option>
            <option value="16">16次</option>
            <option value="17">17次</option>
            <option value="18">18次</option>
            <option value="19">19次</option>
            <option value="20">20次或以上</option>
          </select>
          <div class="deleteThisLine" onclick='deleteThisZhengxin(this, event)'><img src="/Application/Admin/View/Public/images/set_chanpin_popup/u566.png" alt="u566"></div>
          <input type="hidden" id='zhengxin_check_line_id_${data.cinv_times_id}' class="zhengxin_check_line_id" name="zhengxin_check_line_id" value='${data.cinv_times_id}'>
          <input form='hidden_color_placeholder' class='zhengxin_check_color' id="zhengxin_check_${data.cinv_times_id}" type="hidden" name="color" value="" />
        </div>
        <script>
          $('#zhengxin_check_${data.cinv_times_id}').colorPicker()
        </script>
      `.trim()
      $('.zhengxin_check_box').append(templ)
    } else {
      // 请求无法完成
      console.log(data)
    }
  }).fail((err) => {
    // 网络错误
    console.log(err)
  })
}
function addMoreYuqiItem (evt) {
  console.log('请求一行新的征信逾期要求，发送：')
  const postData = {
    yuqi_check_if: $(`#yuqi_check_if`).val(),
    yuqi_check_count_method: $(`#yuqi_check_count_method`).val(),
    yuqi_check_count_cycle: $(`#yuqi_ check_count_cycle`).val(),
    yuqi_check_count_time: $(`#yuqi_check_count_time`).val(),
    yuqi_check_count_source: $('#yuqi_check_count_source').val(),
    cinv_times_color: $(`#yuqi_check_1`).val(),
    g_id: goodsID,
    state: $('.yuqi_check_line').length === 1 ? 1 : 0
  }
  // const postData = $('#zhengxin_hidden_form').serializeArray()
  console.dir(postData)
  $.ajax(evt.data.url.addZhengxinYuqiURL, {
    method: 'post',
    data: postData,
    beforeSend: () => {
      $('.add_more_yuqi_item').css('display', 'none')
    }
  }).done((data) => {
    $('.add_more_yuqi_item').css('display', 'block')
    if (String(data.code) === '1' || String(data.code) === '2') {
      let templ = `
        <div class="yuqi_check_line">
          <div style="width:13.5rem;"></div>
          <input type="num" name="yuqi_check_count_cycle" id='yuqi_check_count_cycle_${data.cinv_see_id}' placeholder="1~36个月"/>
          <div class="placeholder">不能</div>
          <select name="yuqi_check_count_method" id='yuqi_check_count_method_${data.cinv_see_id}'>
            <option value="连续">连续</option>
            <option value="累计">累计</option>
          </select>
          <select name="yuqi_check_count_time" id='yuqi_check_count_time_${data.cinv_see_id}'>
            <option value="1">1个</option>
            <option value="2">2个</option>
            <option value="3">3个</option>
            <option value="4">4个</option>
            <option value="5">5个</option>
            <option value="6">6个</option>
            <option value="7">7个</option>
            <option value="8">8个</option>
            <option value="9">9个</option>
            <option value="10">10个</option>
            <option value="11">11个</option>
            <option value="12">12个</option>
            <option value="13">13个</option>
            <option value="14">14个</option>
            <option value="15">15个</option>
            <option value="16">16个</option>
            <option value="17">17个</option>
            <option value="18">18个</option>
            <option value="19">19个</option>
            <option value="20">20个</option>
          </select>
          <select name="yuqi_check_count_source" id='yuqi_check_count_source_${data.cinv_see_id}'>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
            <option value="6">6</option>
            <option value="7">7</option>
          </select>
          <div class="deleteThisLine" onclick='deleteThisYuqi(this, event)'><img src="/Application/Admin/View/Public/images/set_chanpin_popup/u566.png" alt="u566"></div>
          <input type="hidden" class="yuqi_check_line_id" id='yuqi_check_line_id_${data.cinv_see_id}' name="yuqi_check_line_id" value='${data.cinv_see_id}'>
          <input form='hidden_color_placeholder' class='zhengxin_check_color' id="yuqi_check_${data.cinv_see_id}" type="hidden" name="color" value="#000" />
        </div>
        <script>
          $('#yuqi_check_${data.cinv_see_id}').colorPicker()
        </script>
      `.trim()
      $('.yuqi_check_box').append(templ)
    } else {
      console.log(data)
    }
  })
}
function deleteThisYuqi (_this, evt) {
  const parentEle = $(evt.target).closest('.yuqi_check_line')
  const lineID = parentEle.find('.yuqi_check_line_id').val()
  console.log('请求删除一行征信逾期要求，发送：')
  console.log(`{cinv_see_id: ${lineID}}`)
  $.ajax(String($('#post_remove_zhengxin_yuqi_require').val()), {
    method: 'post',
    data: {cinv_see_id: lineID}
  }).done((data) => {
    if (String(data.code) === '1' || String(data.code) === '2') {
      parentEle.remove()
    } else {
      console.log(data)
    }
  }).fail((err) => {
    console.log(err)
  })
}
function deleteThisZhengxin (_this, evt) {
  const parentEle = $(evt.target).closest('.zhengxin_check_line')
  const lineID = parentEle.find('.zhengxin_check_line_id').val()
  console.log('请求删除一行征信要求，发送：')
  console.log(`{cinv_times_id: ${lineID}}`)
  $.ajax(String($('#post_remove_zhengxin_require').val()), {
    method: 'post',
    data: {cinv_times_id: lineID}
  }).done((data) => {
    if (String(data.code) === '1' || String(data.code) === '2') {
      parentEle.remove()
    } else {
      console.log(data)
    }
  }).fail((err) => {
    console.log(err)
  })
}

function submitZhengxin () {
  let postData = []
  const allLineEle = $('.zhengxin_check_line')
  allLineEle.find('input').each((index, item) => {
    let value = String($(item).val()).replace(/[\D]/, '')
    if (value === '') {
      alert('征信表单未填写完毕')
      throw new Error('征信表单未填写完毕')
    }
    if ($(item).attr('type') === 'num' && (Number(value) < 1 || Number(value) > 36)) {
      alert('征信表单未正确填写完毕')
      throw new Error('征信表单未正确填写完毕')
    }
  })
  allLineEle.each((index, item) => {
    if (index === 0) {
      const ID = $(item).find(`.zhengxin_check_line_id`).val()
      const itemData = {
        zhengxin_check_if: $(`#zhengxin_check_if`).val(),
        // 数组转字符串，多选下拉
        zhengxin_check_count_method: Array.from($(`#zhengxin_check_count_method`).val()).join(','),
        zhengxin_check_count_cycle: $(`#zhengxin_check_count_cycle`).val(),
        zhengxin_check_count_time: $(`#zhengxin_check_count_time`).val(),
        cinv_times_color: $(`#zhengxin_check_1`).val(),
        g_id: goodsID,
        cinv_times_id: ID,
        state: 1
      }
      postData.push(itemData)
    } else {
      const ID = $(item).find(`.zhengxin_check_line_id`).val()
      const itemData = {
        zhengxin_check_if: $(`#zhengxin_check_if`).val(),
        zhengxin_check_count_method: Array.from($(`#zhengxin_check_count_method`).val()).join(','),
        zhengxin_check_count_cycle: $(`#zhengxin_check_count_cycle_${ID}`).val(),
        zhengxin_check_count_time: $(`#zhengxin_check_count_time_${ID}`).val(),
        cinv_times_color: $(`#zhengxin_check_${ID}`).val(),
        g_id: goodsID,
        cinv_times_id: ID,
        state: 0
      }
      postData.push(itemData)
    }
  })
  console.dir(JSON.parse(JSON.stringify(postData)))
  $.ajax(String($('#post_updata_zhengxin_require').val()), {
    method: 'post',
    data: {data: postData},
    async: false
  }).done((data) => {
    if (String(data.code) === '1' || String(data.code) === '2') {
      console.log(data)
    } else {
      console.log(data)
      alert('服务器无法接受信息')
      throw new Error('服务器无法接受信息')
    }
  }).fail((err) => {
    console.log(err)
    alert('无法提交征信信息')
    throw err
  })
}

function submtZhengxinYuqi () {
  let postData = []
  const allLineEle = $('.yuqi_check_line')
  allLineEle.find('input').each((index, item) => {
    let value = String($(item).val()).replace(/\D/, '')
    if (value === '') {
      alert('征信表单未填写完毕')
      throw new Error('征信表单未填写完毕')
    }
    if ($(item).attr('type') === 'num' && (Number(value) < 1 || Number(value) > 36)) {
      alert('征信表单未正确填写完毕')
      throw new Error('征信表单未正确填写完毕')
    }
  })
  allLineEle.each((index, item) => {
    if (index === 0) {
      const ID = $(item).find(`.yuqi_check_line_id`).val()
      const itemData = {
        cinv_see_id: ID,
        yuqi_check_if: $(`#yuqi_check_if`).val(),
        yuqi_check_count_method: $(`#yuqi_check_count_method`).val(),
        yuqi_check_count_cycle: $(`#yuqi_check_count_cycle`).val(),
        yuqi_check_count_time: $(`#yuqi_check_count_time`).val(),
        yuqi_check_count_source: $('#yuqi_check_count_source').val(),
        cinv_see_color: $(`#yuqi_check_1`).val(),
        g_id: goodsID,
        state: 1
      }
      postData.push(itemData)
    } else {
      const ID = $(item).find(`.yuqi_check_line_id`).val()
      const itemData = {
        cinv_see_id: ID,
        yuqi_check_if: $(`#yuqi_check_if`).val(),
        yuqi_check_count_method: $(`#yuqi_check_count_method_${ID}`).val(),
        yuqi_check_count_cycle: $(`#yuqi_check_count_cycle_${ID}`).val(),
        yuqi_check_count_time: $(`#yuqi_check_count_time_${ID}`).val(),
        yuqi_check_count_source: $(`#yuqi_check_count_source_${ID}`).val(),
        cinv_see_color: $(`#yuqi_check_${ID}`).val(),
        g_id: goodsID,
        state: 0
      }
      postData.push(itemData)
    }
  })
  console.dir(postData)
  $.ajax(String($('#post_updata_zhengxin_yuqi_require').val()), {
    method: 'post',
    data: {data: postData},
    async: false
  }).done((data) => {
    if (String(data.code) === '1' || String(data.code) === '2') {
      console.log(data)
    } else {
      console.error('失败')
      alert('服务器无法接受信息')
      throw new Error('服务器无法接受信息')
    }
  }).fail((err) => {
    alert('无法提交征信信息')
    throw new Error(err)
  })
}

function rewriteZhengxin () {
  if (window.altFormData.altFormDataZhengxin === '') {
    throw new Error(`不存在征信回填数据，停止回填操作`)
  }
  const templValue = JSON.parse(window.altFormData.altFormDataZhengxin)
  /**
   *
   * let altFormDataZhengxiYuqi = `[{"cinv_see_id":"7","yuqi_check_if":"1","yuqi_check_count_cycle":"2","yuqi_check_count_method":"\u7d2f\u8ba1","yuqi_check_count_time":"19","yuqi_check_count_source":"2","g_id":"25","state":"1","cinv_see_color":"#ff0000"},{"cinv_see_id":"8","yuqi_check_if":"1","yuqi_check_count_cycle":"33","yuqi_check_count_method":"\u7d2f\u8ba1","yuqi_check_count_time":"19","yuqi_check_count_source":"2","g_id":"25","state":"0","cinv_see_color":"#808000"}]`;
   * let altFormDataZhengxin = `[{"cinv_times_id":"7","zhengxin_check_if":"1","zhengxin_check_count_method":"\u4fe1\u7528\u5361\u67e5\u8be2","zhengxin_check_count_cycle":"1","zhengxin_check_count_time":"18","cinv_times_color":"#ff6600","g_id":"25","state":"1"},{"cinv_times_id":"8","zhengxin_check_if":"1","zhengxin_check_count_method":"\u4fe1\u7528\u5361\u67e5\u8be2","zhengxin_check_count_cycle":"22","zhengxin_check_count_time":"18","cinv_times_color":"#800080","g_id":"25","state":"0"}]`;
   * window.altFormData = {
   *   altFormDataZhengxin: altFormDataZhengxin,
   *   altFormDataZhengxiYuqi: altFormDataZhengxiYuqi
   * }
   */
  $.each(templValue, (index, item) => {
    if (index === 0) {
      const firstLineTempl = `
        <div class="zhengxin_check_box">
          <div class="zhengxin_check_line">
            <select name="zhengxin_check_if" id="zhengxin_check_if">
              <option value="0">不看查询次数</option>
              <option value="1">看查询次数</option>
            </select>
            <div class="zhengxin_check_count_method_wrap">
              <label for="x_zhengxin_zhengxin_check_count_method">
                <div>查询计算标准<br>(ctrl键可多选)</div>
              </label>
              <input style='display:none;' type="checkbox" name="调整是否显示" id='x_zhengxin_zhengxin_check_count_method'>
              <select name="zhengxin_check_count_method" id="zhengxin_check_count_method" multiple>
                <option value="贷款申请" checked>贷款申请</option>
                <option value="信用卡查询">信用卡查询</option>
                <option value="个人查询">个人查询</option>
                <option value="保前审批">保前审批</option>
              </select>
            </div>
            <input type="num" name="zhengxin_check_count_cycle" id="zhengxin_check_count_cycle" placeholder="1~36个月"/>
            <div class="placeholder">不能超过</div>
            <select name="zhengxin_check_count_time" id="zhengxin_check_count_time">
              <option value="1" checked>1次</option>
              <option value="2">2次</option>
              <option value="3">3次</option>
              <option value="4">4次</option>
              <option value="5">5次</option>
              <option value="6">6次</option>
              <option value="7">7次</option>
              <option value="8">8次</option>
              <option value="9">9次</option>
              <option value="10">10次</option>
              <option value="11">11次</option>
              <option value="12">12次</option>
              <option value="13">13次</option>
              <option value="14">14次</option>
              <option value="15">15次</option>
              <option value="16">16次</option>
              <option value="17">17次</option>
              <option value="18">18次</option>
              <option value="19">19次</option>
              <option value="20">20次或以上</option>
            </select>
            <div class="add_more_zhengxin_item"><img src="/Application/Admin/View/Public/images/set_chanpin_popup/u546.png" alt="u546"></div>
            <input type="hidden" class="zhengxin_check_line_id" name="zhengxin_check_line_id" value='${item.cinv_times_id}'>
            <input form='hidden_color_placeholder' class='zhengxin_check_color' id="zhengxin_check_1" type="hidden" name="color" value="${item.cinv_times_color}" />
            <script>
              const addZhengxinURL = String($('#post_new_zhengxin_require').val())
              $('#zhengxin_check_1').colorPicker()
              $('.add_more_zhengxin_item').on({click: addMoreZhengxinItem}, {
                url: {
                  addZhengxinURL: addZhengxinURL,
                }
              })
              $('#zhengxin_check_if').val('${item.zhengxin_check_if}')
              $('#zhengxin_check_count_method').val('${item.zhengxin_check_count_method}'.split(','))
              $('#zhengxin_check_count_time').val('${item.zhengxin_check_count_time}')
              $('#zhengxin_check_count_cycle').val('${item.zhengxin_check_count_cycle}')
            </script>
          </div>
        </div>
      `.trim()
      $('.zhengxin_check_box').replaceWith(firstLineTempl)
    } else {
      const templ = `
        <div class="zhengxin_check_line">
          <div style="width:27.5rem;"></div>
          <input type="num" id='zhengxin_check_count_cycle_${item.cinv_times_id}' name="zhengxin_check_count_cycle" placeholder="1~36个月"/>
          <div class="placeholder">不能超过</div>
          <select name="zhengxin_check_count_time" id='zhengxin_check_count_time_${item.cinv_times_id}'>
            <option value="1">1次</option>
            <option value="2">2次</option>
            <option value="3">3次</option>
            <option value="4">4次</option>
            <option value="5">5次</option>
            <option value="6">6次</option>
            <option value="7">7次</option>
            <option value="8">8次</option>
            <option value="9">9次</option>
            <option value="10">10次</option>
            <option value="11">11次</option>
            <option value="12">12次</option>
            <option value="13">13次</option>
            <option value="14">14次</option>
            <option value="15">15次</option>
            <option value="16">16次</option>
            <option value="17">17次</option>
            <option value="18">18次</option>
            <option value="19">19次</option>
            <option value="20">20次或以上</option>
          </select>
          <div class="deleteThisLine" onclick='deleteThisZhengxin(this, event)'><img src="/Application/Admin/View/Public/images/set_chanpin_popup/u566.png" alt="u566"></div>
          <input type="hidden" id='zhengxin_check_line_id_${item.cinv_times_id}' class="zhengxin_check_line_id" name="zhengxin_check_line_id" value='${item.cinv_times_id}'>
          <input form='hidden_color_placeholder' class='zhengxin_check_color' id="zhengxin_check_${item.cinv_times_id}" type="hidden" name="color" value="${item.cinv_times_color}" />
        </div>
        <script>
          $('#zhengxin_check_${item.cinv_times_id}').colorPicker()
          $('#zhengxin_check_count_time_${item.cinv_times_id}').val('${item.zhengxin_check_count_time}')
          $('#zhengxin_check_count_cycle_${item.cinv_times_id}').val('${item.zhengxin_check_count_cycle}')
        </script>
      `.trim()
      $('.zhengxin_check_box').append(templ)
    }
  })
}

function rewriteZhengxinYuqi () {
  if (window.altFormData.altFormDataZhengxiYuqi === '') {
    throw new Error(`不存在征信逾期回填数据，停止回填操作`)
  }
  const templValue = JSON.parse(window.altFormData.altFormDataZhengxiYuqi)
  $.each(templValue, (index, item) => {
    if (index === 0) {
      const ramdom = ~~(Math.random() * 1e5)
      const firstLineTempl = `
        <div class="yuqi_check_box">
          <div class="yuqi_check_line">
            <select name="yuqi_check_if" id="yuqi_check_if">
              <option value="0" checked>不看人行征信</option>
              <option value="1">看人行征信</option>
            </select>
            <input type="num" name="yuqi_check_count_cycle" id="yuqi_check_count_cycle" placeholder="1~36个月"/>
            <div class="placeholder">不能</div>
            <select name="yuqi_check_count_method" id="yuqi_check_count_method">
              <option value="连续">连续</option>
              <option value="累计">累计</option>
            </select>
            <select name="yuqi_check_count_time" id="yuqi_check_count_time">
              <option value="1" checked>1个</option>
              <option value="2">2个</option>
              <option value="3">3个</option>
              <option value="4">4个</option>
              <option value="5">5个</option>
              <option value="6">6个</option>
              <option value="7">7个</option>
              <option value="8">8个</option>
              <option value="9">9个</option>
              <option value="10">10个</option>
              <option value="11">11个</option>
              <option value="12">12个</option>
              <option value="13">13个</option>
              <option value="14">14个</option>
              <option value="15">15个</option>
              <option value="16">16个</option>
              <option value="17">17个</option>
              <option value="18">18个</option>
              <option value="19">19个</option>
              <option value="20">20个</option>
            </select>
            <select name="yuqi_check_count_source" id="yuqi_check_count_source">
              <option value="1" checked>1</option>
              <option value="2">2</option>
              <option value="3">3</option>
              <option value="4">4</option>
              <option value="5">5</option>
              <option value="6">6</option>
              <option value="7">7</option>
            </select>
            <div class="add_more_yuqi_item"><img src="/Application/Admin/View/Public/images/set_chanpin_popup/u546.png" alt="u546"></div>
            <input type="hidden" class="yuqi_check_line_id" name="yuqi_check_line_id" value='${item.cinv_see_id}'>
            <input form='hidden_color_placeholder' class='zhengxin_check_color' id="yuqi_check_1" type="hidden" name="color" value="${item.cinv_see_color}" />
            <script>

              const addZhengxinYuqiURL${ramdom} = String($('#post_new_zhengxin_yuqi_require').val())
              const URLObject${ramdom} = {
                addZhengxinYuqiURL: addZhengxinYuqiURL${ramdom},
              }
              $('#yuqi_check_1').colorPicker()
              $('.add_more_yuqi_item').on({click: addMoreYuqiItem}, {url: URLObject${ramdom}})
              $('#yuqi_check_if').val('${item.yuqi_check_if}')
              $('#yuqi_check_count_cycle').val('${item.yuqi_check_count_cycle}')
              $('#yuqi_check_count_method').val('${item.yuqi_check_count_method}')
              $('#yuqi_check_count_time').val('${item.yuqi_check_count_time}')
              $('#yuqi_check_count_source').val('${item.yuqi_check_count_source}')
            </script>
          </div>
        </div>
      `.trim()
      $('.yuqi_check_box').replaceWith(firstLineTempl)
    } else {
      let templ = `
        <div class="yuqi_check_line">
          <div style="width:13.5rem;"></div>
          <input type="num" name="yuqi_check_count_cycle" id='yuqi_check_count_cycle_${item.cinv_see_id}' placeholder="1~36个月"/>
          <div class="placeholder">不能</div>
          <select name="yuqi_check_count_method" id='yuqi_check_count_method_${item.cinv_see_id}'>
            <option value="连续">连续</option>
            <option value="累计">累计</option>
          </select>
          <select name="yuqi_check_count_time" id='yuqi_check_count_time_${item.cinv_see_id}'>
            <option value="1">1个</option>
            <option value="2">2个</option>
            <option value="3">3个</option>
            <option value="4">4个</option>
            <option value="5">5个</option>
            <option value="6">6个</option>
            <option value="7">7个</option>
            <option value="8">8个</option>
            <option value="9">9个</option>
            <option value="10">10个</option>
            <option value="11">11个</option>
            <option value="12">12个</option>
            <option value="13">13个</option>
            <option value="14">14个</option>
            <option value="15">15个</option>
            <option value="16">16个</option>
            <option value="17">17个</option>
            <option value="18">18个</option>
            <option value="19">19个</option>
            <option value="20">20个</option>
          </select>
          <select name="yuqi_check_count_source" id='yuqi_check_count_source_${item.cinv_see_id}'>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
            <option value="6">6</option>
            <option value="7">7</option>
          </select>
          <div class="deleteThisLine" onclick='deleteThisYuqi(this, event)'><img src="/Application/Admin/View/Public/images/set_chanpin_popup/u566.png" alt="u566"></div>
          <input type="hidden" class="yuqi_check_line_id" id='yuqi_check_line_id_${item.cinv_see_id}' name="yuqi_check_line_id" value='${item.cinv_see_id}'>
          <input form='hidden_color_placeholder' class='zhengxin_check_color' id="yuqi_check_${item.cinv_see_id}" type="hidden" name="color" value="${item.cinv_see_color}" />
        </div>
        <script>
          $('#yuqi_check_${item.cinv_see_id}').colorPicker()
          $('#yuqi_check_count_cycle_${item.cinv_see_id}').val('${item.yuqi_check_count_cycle}')
          $('#yuqi_check_count_method_${item.cinv_see_id}').val('${item.yuqi_check_count_method}')
          $('#yuqi_check_count_time_${item.cinv_see_id}').val('${item.yuqi_check_count_time}')
          $('#yuqi_check_count_source_${item.cinv_see_id}').val('${item.yuqi_check_count_source}')
        </script>
      `.trim()
      $('.yuqi_check_box').append(templ)
    }
  })
}
